#include "tabscreen.h"
#include "whaletabbase.h"

#include <QList>
#include <QTimer>
#include <QtDebug>

TabScreen::TabScreen( QWidget * parent ) : QScrollArea( parent ), mLastTab( 0 ) {
    this->mTabsLayout = new QHBoxLayout();
    this->mTabsLayout->setAlignment( Qt::AlignLeft );
    this->mTabsLayout->setSizeConstraint( QLayout::SetMinAndMaxSize );

    QWidget * tabContainer = new QWidget();
    tabContainer->setLayout( this->mTabsLayout );
    tabContainer->setObjectName( "centralWidget" );

    this->setWidget( tabContainer );
    this->setWidgetResizable( true );
    this->setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
    this->setObjectName( "tabsArea" );
}

void TabScreen::addTab( WhaleTabBase * tab ) {
    this->mTabsLayout->addWidget( tab );
    this->mLastTab = tab;
    QTimer::singleShot( 5, this, SLOT( scrollToEnd() ) );
}

void TabScreen::scrollToEnd() {
    this->ensureWidgetVisible( this->mLastTab );
}

void TabScreen::tabClosed( WhaleTabBase * tab ) {
    this->mTabsLayout->removeWidget( tab );
}

QLayout * TabScreen::tabsLayout() {
    return this->mTabsLayout;
}
